package com.itheima.atm;

/**
 * @author Anomaly
 * @date 2022/12/6 22:50
 */

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * ATM系统入口
 */
public class ATMSystem {
    public static void main(String[] args) {
        ArrayList<Account> accounts = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("=================ATM系统=================");
            System.out.println("1. 账户登录");
            System.out.println("2. 账户注册");
            System.out.println("请选择操作：");
            int cmd = scanner.nextInt();
            if (cmd == 1) {
                login(accounts, scanner);
            } else if (cmd == 2) {
                register(accounts, scanner);
            } else {
                System.out.println("您选择的功能不存在！请重新输入");
            }
        }
    }

    /**
     * 登录功能
     *
     * @param accounts
     * @param scanner
     */
    private static void login(ArrayList<Account> accounts, Scanner scanner) {
        System.out.println("=======================登录功能=====================");
        System.out.println("请输入卡号：");
        String cardId = scanner.next();
        Account account = getAccountByCardId(accounts, cardId);
        if (account == null) {
            System.out.println("该用户不存在！");
        } else {
            while (true) {
                System.out.println("请输入密码：");
                String password = scanner.next();
                if (password.equals(account.getPassword())) {
                    System.out.println("正在登录中...");
                    System.out.println("登录成功！" + account.getName() + "，卡号：" + account.getCardId());
                    UserCommand(accounts, account, scanner);
                    break;
                } else {
                    System.out.println("密码错误，登录失败！！！");
                }
            }
        }
    }

    /**
     * 用户登录后操作
     *
     * @param accounts
     * @param account
     * @param scanner
     */
    private static void UserCommand(ArrayList<Account> accounts, Account account, Scanner scanner) {
        while (true) {
            System.out.println("================用户操作=================");
            System.out.println("1. 查询账户信息");
            System.out.println("2. 存款");
            System.out.println("3. 取款");
            System.out.println("4. 转账");
            System.out.println("5. 修改密码");
            System.out.println("6. 退出");
            System.out.println("7. 注销账户");
            System.out.println("请选择操作：");
            int cmd = scanner.nextInt();
            if (cmd == 1) {
                showAccount(account);
            } else if (cmd == 2) {
                depositMoney(account, scanner);
            } else if (cmd == 3) {
                drawMoney(account, scanner);
            } else if (cmd == 4) {
                transferMoney(accounts, account, scanner);
            } else if (cmd == 5) {
                if (updatePassword(account, scanner)) {
                    break;
                }
            } else if (cmd == 6) {
                break;
            } else if (cmd == 7) {
                if (removeAccount(accounts, account, scanner)) {
                    break;
                }
            } else {
                System.out.println("该功能不存在，请重新输入！");
            }
        }
    }

    /**
     * 销户功能
     *
     * @param accounts
     * @param account
     * @param scanner
     */
    private static boolean removeAccount(ArrayList<Account> accounts, Account account, Scanner scanner) {
        System.out.println("确认注销账户吗？ y/n");
        String s = scanner.next();
        if (s.equals("y")) {
            accounts.remove(account);
            System.out.println("注销成功！");
            return true;
        } else {
            System.out.println("注销失败！");
            return false;
        }
    }

    /**
     * 修改密码
     *
     * @param account
     * @param scanner
     */
    private static boolean updatePassword(Account account, Scanner scanner) {
        System.out.println("===================修改密码功能===================");
        System.out.println("请输入当前密码");
        String pass = scanner.next();
        if (pass.equals(account.getPassword())) {
            while (true) {
                System.out.println("请输入新密码：");
                String pass1 = scanner.next();
                System.out.println("请确认新密码：");
                String pass2 = scanner.next();
                if (pass1.equals(pass2)) {
                    System.out.println("密码修改成功，请重新登录");
                    account.setPassword(pass1);
                    return true;
                } else {
                    System.out.println("两次输入不一致，请重新输入");
                }
            }
        } else {
            System.out.println("密码错误！");
            return false;
        }
    }

    /**
     * 转账功能
     *
     * @param accounts
     * @param account
     * @param scanner
     */
    private static void transferMoney(ArrayList<Account> accounts, Account account, Scanner scanner) {
        System.out.println("===================转账功能===================");
        System.out.println("请输入目标账户卡号：");
        String cardId = scanner.next();
        Account account1 = getAccountByCardId(accounts, cardId);
        if (account1 == null) {
            System.out.println("该卡号不存在！");
        } else {
            System.out.println("请确认对方信息：");
            System.out.println("用户名：" + account1.getName() + "，卡号：" + account1.getCardId());
            System.out.println("请输入转账金额：");
            double money = scanner.nextDouble();
            if (money < 0) {
                System.out.println("输入有误！");
            } else if (money > account.getQuotaMoney()) {
                System.out.println("超过单次限额！");
            } else if (money > account.getMoney()) {
                System.out.println("对不起，您的余额不足" + money + "元！");
            } else {
                account.setMoney(account.getMoney() - money);
                account1.setMoney(account1.getMoney() + money);
                System.out.println("恭喜您成功转账" + money + "元！");
            }
        }
    }

    /**
     * 取钱
     *
     * @param account
     * @param scanner
     */
    private static void drawMoney(Account account, Scanner scanner) {
        System.out.println("================用户取钱操作=================");
        System.out.println("输入您要取出的金额：");
        double money = scanner.nextDouble();
        if (money < 0) {
            System.out.println("您的输入有误！");
        } else if (money > account.getQuotaMoney()) {
            System.out.println("超过单次限额！");
        } else if (money > account.getMoney()) {
            System.out.println("对不起，您的余额不足" + money + "元！");
        } else {
            account.setMoney(account.getMoney() - money);
            System.out.println("恭喜您成功取出" + money + "元！");
        }
    }

    /**
     * 存款
     *
     * @param account
     * @param scanner
     */
    private static void depositMoney(Account account, Scanner scanner) {
        System.out.println("================用户存钱操作=================");
        System.out.println("请输入您的存入金额：");
        double money = scanner.nextDouble();
        if (money >= 0) {
            account.setMoney(account.getMoney() + money);
            System.out.println("恭喜您成功存款" + money + "元！");
        } else {
            System.out.println("输入有误！");
        }

    }

    /**
     * 展示账户信息
     *
     * @param account
     */
    private static void showAccount(Account account) {
        System.out.println("======================当前账户信息");
        System.out.println("卡号：" + account.getCardId());
        System.out.println("用户：" + account.getName());
        System.out.println("余额：" + account.getMoney());
        System.out.println("限额：" + account.getQuotaMoney());
    }

    /**
     * 用户开户功能
     *
     * @param accounts 账户集合
     * @param scanner
     */
    private static void register(ArrayList<Account> accounts, Scanner scanner) {
        System.out.println("==================开户操作==================");
        Account account = new Account();
        System.out.println("请输入用户名：");
        String name = scanner.next();
        account.setName(name);
        while (true) {
            System.out.println("请输入密码：");
            String password = scanner.next();
            System.out.println("请确认密码：");
            String password2 = scanner.next();
            if (password.equals(password2)) {
                account.setPassword(password);
                break;
            } else {
                System.out.println("两次输入的密码不一致！");
            }
        }
        System.out.println("请您输入账户单次限额：");
        account.setQuotaMoney(scanner.nextDouble());

        System.out.println("正在等待开户...");
        String cardId = getRandomCardId(accounts);
        account.setCardId(cardId);
        accounts.add(account);
        System.out.println("恭喜您，" + name + "，开户成功！您的卡号是：" + cardId);
    }

    /**
     * 生成8位卡号
     *
     * @param accounts
     * @return
     */
    private static String getRandomCardId(ArrayList<Account> accounts) {
        Random random = new Random();
        while (true) {
            String cardId = "";
            for (int i = 0; i < 8; i++) {
                cardId += random.nextInt(10);
            }
            if (getAccountByCardId(accounts, cardId) == null) {
                return cardId;
            }
        }
    }

    /**
     * 根据卡号查询账户对象
     *
     * @param accounts
     * @param cardId
     * @return
     */
    private static Account getAccountByCardId(ArrayList<Account> accounts, String cardId) {
        for (Account account : accounts) {
            if (cardId.equals(account.getCardId())) {
                return account;
            }
        }
        return null;
    }
}
